<?php
/**
 * $Id:$
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


class htmlForm {
	
	var $name = '__none__';
	var $fields = array();
	var $section = false;
	var $valid = true;
	var $default_values = array();
	var $have_submit = false;
	var $is_multipart = false;

	function htmlForm($name = '__none__') {
		$this->name = $name;
	}

	function clean() {
		$this->fields = array();
		$this->section = false;
	}

	function isValid() {
		return $this->valid;
	}

	function isPost() {
		if (!empty($_POST[$this->name]['__F__']))
			return true;
		return false;
	}

	function isCancel() {
		if (!empty($_POST[$this->name]['__C__']))
			return true;
		return false;
	}

	function get($key) {
		if (!$this->isPost()) {
			if (isset($this->default_values[$key]))
				return $this->default_values[$key];
			return null;
		}
		if (isset($_POST[$this->name][$key]))
			return $_POST[$this->name][$key];
		return false;
	}

	function getFile($key) {
		if ($_FILES[$this->name]['error'][$key] == 0)
			return array(
				'name' => $_FILES[$this->name]['name'][$key],
				'type' => $_FILES[$this->name]['type'][$key],
				'tmp_name' => $_FILES[$this->name]['tmp_name'][$key], 
				'size' => $_FILES[$this->name]['size'][$key], 
			);
		return false;
	}

	function addSection($section) {
		$this->section = $section;
	}

	function addField($title, $name, $type, $require = false, $check_type = 'none', $help = '') {
		$value = $this->get($name);
		$notice = false;

		if ($this->isPost()) {
			if (empty($value) && $require) {
				$notice = tr('You must fillin this entry');
				$this->valid = false;
			} else if (!empty($value)) {
				switch($check_type) {
					case 'text255': 
						if (strlen($value) <= 0 && strlen($value) > 255)
							$notice = tr('Invalid field (text255)');
						break;
					case 'md5': 
						if (strlen($value) <= 0 && strlen($value) > 31)
							$notice = tr('Invalid field (md5)');
						break;
					case 'date':
						if (!ereg('^([0-9]{2})/([0-9]{2})/([0-9]{4})$', $value))
							$notice = tr('Invalid date');
					case 'email':
						if (!ereg('^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int|biz|info|aero)\$', $value))
							$notice = tr('Invalid email');
				}
			}
		}

		if ($type == 'file')
			$this->is_multipart = true;

		$this->fields[] = array(
			'section' => $this->section,
			'title' => $title . ($require ? ' * ' : ''),
			'name'	=> $name,
			'type'	=> $type,
			'notice' => $notice,
			'value'	=> $value,
			'help' => $help,
		);

	}

	function addSelect($title, $name, $require = false, $options = null, $help = '', $redirect = false) {
		$value = $this->get($name);
		$notice = false;

		if ($this->isPost()) {
			if (empty($value) && $require) {
				$notice = 'Ce champ doit être rempli';
				$this->valid = false;
			}
		}

		$this->fields[] = array(
			'section' => $this->section,
			'title' => $title . ($require ? ' * ' : ''),
			'name'	=> $name,
			'type'	=> 'select',
			'notice' => $notice,
			'value'	=> $value,
			'options' => $options,
			'help' => $help,
			'redirect' => $redirect
		);

	}

	function set($name, $value) {
		foreach ($this->fields as $k => $f) {
			if ($f['name'] == $name) {
				$this->fields[$k]['value'] = $value;
				return;
			}
		}
	}

	function addText($text) {
		$this->fields[] = array(
			'section' => $this->section,
			'text'	=> $text,
			'type'	=> 'none',
		);
	}

	function addSubmit($title = 'Send') {
		$this->fields[] = array(
			'section' => $this->section,
			'title'	=> $title,
			'type'	=> 'submit',
			'name'	=> '__F__',
		);
		$this->have_submit = true;
	}

	function formatNotice($notice) {
		if ($notice)
			return sprintf('<br/><span style="color: red">%s</span>', $notice);
		return '';
	}

	function output_open() {
		printf('<form method="POST"%s>', $this->is_multipart ? 'enctype="multipart/form-data"' : '');
	}

	function output_content() {

		$section = -1;
	
		foreach ($this->fields as $field) {

			if ($section != $field['section']) {
				if ($section != null)
					echo '</table>';
				echo sprintf('<h2>%s</h2>', $field['section']);
				echo '<table>';
				$section = $field['section'];
			}

			$name = false;
			if (!empty($field['name']))
				$name = sprintf('%s[%s]', $this->name, $field['name']);
		
			switch ($field['type']) {
	
				case 'text':
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><input type="text" class="field" name="%s" value="%s"/>&nbsp;%s%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						($field['value']),
						$field['help'],
						$this->formatNotice($field['notice'])
					);
					break;
			
				case 'file':
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><input type="file" class="field" name="%s"/>&nbsp;%s%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						$field['help'],
						$this->formatNotice($field['notice'])
					);
					break;
			
				case 'textarea':
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><textarea class="field" name="%s">%s</textarea>%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						($field['value']),
						$this->formatNotice($field['notice'])
					);
					break;
			
				case 'password':
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><input type="password" class="field" name="%s" value="%s"/>%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						($field['value']),
						$this->formatNotice($field['notice'])
					);
					break;
	
				case 'checkbox':
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><input type="checkbox" class="field" name="%s" value="1" %s/>%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						empty($field['value']) ? '' : 'checked',
						$this->formatNotice($field['notice'])
					);
					break;

				case 'select':
					$html_onselect = '';
					if ($field['redirect']) {
						$html_onselect = sprintf('onchange="gt(\'?p=%s/\', this.value)"', $field['redirect']);
					}
					$html_options = array();
					if (is_array($field['options'])) {
						foreach ($field['options'] as $opt) {
							$html_options[] = sprintf('<option value="%s" class="%s"%s>%s</option>',
								$opt['id'],
								$opt['class'],
								($opt['id'] == $field['value']) ? ' selected' : '',
								$opt['title']
							);
						}
					}
					printf('<tr>
							<td><label for="%s">%s</label></td>
							<td><select class="field" name="%s" %s/>%s</select>&nbsp;%s%s</td>
						</tr>', 
						$name,
						$field['title'],
						$name,
						$html_onselect,
						implode("\n", $html_options),
						$field['help'],
						$this->formatNotice($field['notice'])
					);
					break;

				case 'submit':
					printf('<tr><td colspan="2"><input type="submit" name="%s" value="%s"/> <input type="submit" name="%s[__C__]" value="%s"/></td></tr>',
						$name,
						$field['title'],
						$this->name,
						tr('Cancel')
					);
					break;
					
				case 'none':
					printf('<tr><td colspan="2" class="text">%s</td></tr>', $field['text']);
			}
			
		}

		echo '</table>';
	}

	function output_close() {
		if (!$this->have_submit) 
			echo '<input type="hidden" name="__F__" value="1"/>';
		echo '</form>';
	}

	function output() {
		$this->output_open();
		$this->output_content();
		$this->output_close();
	}

	function get_output() {
		ob_start();
		$this->output();
		$data = ob_get_contents();
		ob_end_clean();
		return $data;
	}
	
}

?>
